﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace VideoFile
{
    public static class FilePath
    {

        static string RootAraaDevicePath(string root, string area, string device)
        {
            return root + "\\" + area + "\\" + device + "\\";
        }

        static string DataPath(DateTime DT)
        {
            return string.Format("{0}\\{1:D2}\\{2:D2}\\", DT.Year, DT.Month, DT.Day);
        }

        static string YearMonthPath(DateTime DT) 
        { 
            return string.Format("{0}\\{1:D2}\\", DT.Year, DT.Month); 
        }


        static string TimeString(DateTime DT)
        {
            return DT.ToString("HH-mm-ss-fff");
        }

        static DateTime DatePathTimeFile(string path)
        {
            string[] dts = path.Split('\\', '/', '.','-');
            if (dts.Length < 8) throw new ApplicationException("Path error!|DatePathTimeFile");
            int millisecond = int.Parse(dts[dts.Length - 2]);
            int second = int.Parse(dts[dts.Length - 3]);
            int minute = int.Parse(dts[dts.Length - 4]);
            int hour = int.Parse(dts[dts.Length - 5]);
            int day = int.Parse(dts[dts.Length - 6]);
            int month = int.Parse(dts[dts.Length - 7]);
            int year = int.Parse(dts[dts.Length - 8]);
            DateTime DT = new DateTime(year, month, day, hour, minute, second, millisecond);

            return DT;
        }


        public static List<int> GetYears(string area, string device, params string[] roots)
        {
            List<int> years = new List<int>();
            foreach (var root in roots)
            {
                DirectoryInfo DI = new DirectoryInfo(RootAraaDevicePath(root, area, device));
                if (!DI.Exists) continue;
                DirectoryInfo[] SubDIs = DI.GetDirectories();
                foreach (var year in SubDIs)
                {
                    int y = 0;
                    if (int.TryParse(year.Name, out y) && !years.Contains(y)) years.Add(y);
                }
            }
            years.Sort();
            return years;
        }

        public static List<int> GetMonths(string area, string device, int year, params string[] roots)
        {
            List<int> months = new List<int>();
            foreach (var root in roots)
            {
                DirectoryInfo DI = new DirectoryInfo(RootAraaDevicePath(root, area, device) + year.ToString());
                if (!DI.Exists) continue;
                DirectoryInfo[] SubDIs = DI.GetDirectories();
                foreach (var month in SubDIs)
                {
                    int m = 0;
                    if (int.TryParse(month.Name, out m) && !months.Contains(m)) months.Add(m);
                }
            }
            months.Sort();
            return months;
        }

        public static List<int> GetDays(string area, string device, int year, int month, params string[] roots)
        {
            List<int> days = new List<int>();
            foreach (var root in roots)
            {
                DirectoryInfo DI = new DirectoryInfo(RootAraaDevicePath(root, area, device) + year.ToString() + "\\" + month.ToString("D2"));
                if (!DI.Exists) continue;
                DirectoryInfo[] SubDIs = DI.GetDirectories();
                foreach (var day in SubDIs)
                {
                    int d = 0;
                    if (int.TryParse(day.Name, out d) && !days.Contains(d)) days.Add(d);
                }
            }
            days.Sort();
            return days;
        }


        public static string GetDatePath(string root, string area, string device, DateTime time)
        {
            return RootAraaDevicePath(root, area, device) + DataPath(time);
        }

        public static string GetDateTimePath(string root, string area, string device, DateTime time)
        {
            return RootAraaDevicePath(root, area, device) + DataPath(time) + TimeString(time);
        }

        public static string GetDatePathTimeFile(string root, string area, string device, DateTime time, string postfix)
        {
            return RootAraaDevicePath(root, area, device) + DataPath(time) + TimeString(time) + "." + postfix;
        }

        public static string GetDateTimePathTimeFile(string root, string area, string device, DateTime datetime, DateTime time, string postfix)
        {
            return RootAraaDevicePath(root, area, device) + DataPath(datetime) + TimeString(datetime) + "\\" + TimeString(time) + "." + postfix;//当time与datetime日期不同时可能产生BUG
        }

        public static DateTime[] GetYearMonthPathDate(string root, string area, string device, DateTime time)
        {
            DirectoryInfo DI = new DirectoryInfo(RootAraaDevicePath(root, area, device) + YearMonthPath(time));
            if (!DI.Exists) return new DateTime[0];//throw new ApplicationException("Directory is not exsit!|GetYearMonthPathDirectoryInfo");

            DirectoryInfo[] DIs = DI.GetDirectories();
            List<DateTime> dates = new List<DateTime>();
            foreach (DirectoryInfo di in DIs)
            {
                int day = int.Parse(di.Name);
                if (day > 0 && day < 32) dates.Add(new DateTime(time.Year,time.Month,day));
            }
            return dates.ToArray();

        }

        public static DateTime[] GetDatePathFileInfo(string root, string area, string device, string postfix, DateTime time)
        {
            DirectoryInfo DI = new DirectoryInfo(RootAraaDevicePath(root, area, device) + DataPath(time));

            if (!DI.Exists) return new DateTime[0];//throw new ApplicationException("Directory is not exsit!|GetDatePathFileInfo");

            FileInfo[] FIs = DI.GetFiles("*." + postfix);

            DateTime[] datetimes = new DateTime[FIs.Length];
            for (int i = 0; i < FIs.Length; i++)
            {
                datetimes[i] = DatePathTimeFile(FIs[i].FullName);
            }

            return datetimes;
        }

        public static FileInfo[] GetDateTimePathFileInfo(string root, string area, string device, string postfix, DateTime time)
        {
            throw new NotImplementedException();
        }

    }
}
